package com.bss.ajax;

import java.util.ArrayList;
import java.util.Map;

import com.bss.beans.Attendent;
import com.bss.dao.CheckInOutDao;
import com.bss.framework.FrontController;

public class FindAttendentCommand implements AjaxCommand {

	public synchronized String execute(Map<String, String[]> input) {

		String fewletters = input.get("fewletters")[0];
		
		fewletters = fewletters.toLowerCase();

		//System.out.println((fewletters));

		try {

			ArrayList<Attendent> attList = CheckInOutDao.getAttendentList();
			System.out.println("Total attendents: "+attList.size());

			StringBuilder sb = new StringBuilder();

			for (Attendent at : attList) {
				if (at.getSName().toLowerCase().startsWith(fewletters)) {

					String toAppend = at.getSName() + ","
							+ at.getSRelationship() + "," + at.getSIdType()
							+ "," + at.getSIdNo();
					if (sb.indexOf(toAppend) == -1) {
						sb.append(toAppend);
						sb.append(":");
					}
				}
			}
			return sb.toString();
		} catch (Exception e) {
			FrontController.debug(e.getMessage());
		}
		return null;
	}

}
